// pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiverName: "",
    mobile: "",
    addressDetail: "",
    city: '',
    province: '',
    isDisabled: false,
    isComplete: false,
    buttonTitle: "修改",
    touched: 0,
    isPay: false,
    addrInfo: {},
    payList: [],
  },
  isObjectValueEqual(a, b) {
    var propsCurr = Object.getOwnPropertyNames(a);
    // 要比较的另外一个Object对象
    var propsCompare = Object.getOwnPropertyNames(b);
    if (propsCurr.length != propsCompare.length) {
      return false;
    }
    for (var i = 0, max = propsCurr.length; i < max; i++) {
      var propName = propsCurr[i];
      if (a[propName] !== b[propName]) {
        return false;
      }
    }
    return true;
  },
  test: function () {
    var that = this;
    var attr_item = wx.getStorageSync('attr_item');
    var payList = that.data.payList;
    var arry = [];
    for (var i = 0; i < attr_item.length; i++) {
      for (var j = 0; j < payList.length; j++) {
        if (payList[j].versionId == attr_item[i].versionId &&
          payList[j].select_num == attr_item[i].select_num) {
          payList.splice(j, 1);
          delete attr_item[i];
          break;
        }
      }
    }
    for (var i = 0; i < attr_item.length; i++) {
      if (attr_item[i] != null) {
        arry.push(attr_item[i]);
      }
    }
    wx.removeStorageSync('attr_item')
    wx.setStorageSync('attr_item', arry)
    console.log(attr_item);
    wx.redirectTo({
      url: "/pages/order/order",
    })
  },
  checkOut: function () {
    var that = this;
    if (that.data.receiverName == "" || that.data.mobile == "" || that.data.addressDetail == "" || that.data.city == "" || that.data.province == ""||that.data.buttonTitle=="保存") {
      this.data.isComplete = false;
      wx.showModal({
        title: "提示",
        content: "请完善信息",
        showCancel: false
      });
    }else{
      var Authorization = wx.getStorageSync('Authorization');
    if (Authorization == '') {
      wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
      wx.reLaunch({
        url: '/pages/openid/openid',
      })
    } else {
      that.setData({
        Authorization: Authorization,
      })
    }
    wx.request({
      url: "http://localhost:8080/wechat/pay",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': Authorization
      },
      data: {
        'payList': JSON.stringify(that.data.payList),
        'receiverName': that.data.receiverName,
        'mobile': that.data.mobile,
        'addressDetail': that.data.addressDetail,
        'city': that.data.city,
        'province': that.data.province,
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data['locked'] == 'true') {
            wx.showToast({
              title: res.data['lockedMessage'],
              icon: 'none',
              duration: 3000
            })
          } else {
            var attr_item = wx.getStorageSync('attr_item');
            var payList = that.data.payList;
            var arry = [];
            for (var i = 0; i < attr_item.length; i++) {
              for (var j = 0; j < payList.length; j++) {
                if (payList[j].versionId == attr_item[i].versionId &&
                  payList[j].select_num == attr_item[i].select_num) {
                  payList.splice(j, 1);
                  delete attr_item[i];
                  break;
                }
              }
            }
            for (var i = 0; i < attr_item.length; i++) {
              if (attr_item[i] != null) {
                arry.push(attr_item[i]);
              }
            }
            wx.removeStorageSync('attr_item')
            wx.setStorageSync('attr_item', arry)
            wx.redirectTo({
              url: "/pages/order/order",
            })
          }
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }
      }
    })
    }
  },
  formSubmit(e) {
    const addrInfo = e.detail.value;
    let {
      receiverName,
      mobile,
      province,
      city,
      addressDetail
    } = addrInfo;
    if (receiverName == "" || mobile == "" || addressDetail == "" || city == "" || province == "") {
      this.data.isComplete = false;
      wx.showModal({
        title: "提示",
        content: "请完善信息",
        showCancel: false
      });
    } else if (!/^[1][3,4,5,7,8]\d{9}$/.test(mobile)) {
      wx.showModal({
        title: "提示",
        content: "手机号格式不规范",
        showCancel: false
      });
    } else {
      this.data.isComplete = true;
      wx.setStorageSync('addrInfo', addrInfo);
      let {
        receiverName,
        mobile,
        province,
        city,
        addressDetail
      } = addrInfo;
      this.setData({
        receiverName: receiverName,
        mobile: mobile,
        addressDetail: addressDetail,
        city: city,
        province: province,
        isDisabled: true,
        buttonTitle: "修改"
      });
    }
  },
  updateAddr(e) {
    this.setData({
      isDisabled: false,
      buttonTitle: '保存',
    });
  },
  touched: function (e) {
    console.info("touched");
    this.data.buttonTitle == "修改" ? this.updateAddr() : this.formSubmit(e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let addrInfo = wx.getStorageSync("addrInfo");
    console.log(addrInfo);
    let {
      receiverName,
      mobile,
      province,
      city,
      addressDetail
    } = addrInfo;
    console.log(options.isPay);
    if (options.isPay != null) {
      this.setData({
        isPay: options.isPay,
      })
    }
    this.setData({
      receiverName: receiverName || this.data.receiverName,
      mobile: mobile || this.data.mobile,
      addressDetail: addressDetail || this.data.addressDetail,
      city: city || this.data.city,
      province: province || this.data.province,
      payList: wx.getStorageSync('payList'),
      isDisabled: true,
      isComplete: true,
      buttonTitle: "修改"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})